﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace CountTools
{
    public partial class ChangeCodeName : Form
    {
        private string path;
        private XmlDocument xml;
        private string codeName;

        public ChangeCodeName(string path, string codeName)
        {
            InitializeComponent();
            string dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string xmlPath = Path.Combine(dllPath, "工程量计算规范.xml");
            this.path = path;
            this.codeName = codeName;
            XmlDocument xml = new XmlDocument();
            if (File.Exists(path))
            {
                xml.Load(path);
            }
            else
            {
                xml.Load(xmlPath);
            }
            this.xml = xml;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.CodeName = comboBox3.Text;
        }

        private void ChangeCodeName_Load(object sender, EventArgs e)
        {
            XmlNode childNode = xml.SelectSingleNode("工程量计算规范");


            XmlNodeList childNodeList = childNode.ChildNodes;
            List<string> list1 = new List<string>();
            foreach (XmlNode child1Node in childNodeList)
            {
                list1.Add(child1Node.Name);
            }
            comboBox1.DataSource = list1;

            string projectName = comboBox1.Text;
            XmlNode childNode2 = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName);
            XmlNodeList childNodeList2 = childNode2.ChildNodes;
            List<string> list2 = new List<string>();
            foreach (XmlNode child1Node in childNodeList2)
            {
                list2.Add(child1Node.Name);
            }
            comboBox2.DataSource = list2;

            string codeClass = comboBox2.Text;
            XmlNode childNode3 = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(codeClass);
            XmlNodeList childNodeList3 = childNode3.ChildNodes;
            List<string> list3 = new List<string>();
            foreach (XmlNode child1Node in childNodeList3)
            {
                list3.Add(child1Node.SelectSingleNode("项目编码").InnerText + " " + child1Node.SelectSingleNode("项目名称").InnerText);
            }
            comboBox3.DataSource = list3;


            if (codeName != "未知")
            {
                bool flag = false;
                foreach (XmlNode xmlNode in childNode.ChildNodes)
                {
                    if (flag) break;
                    foreach (XmlNode xmlElement in xmlNode.ChildNodes)
                    {
                        if (flag) break;
                        foreach (XmlNode childElem in xmlElement.ChildNodes)
                        {
                            if (childElem.Name == codeName.Split(' ')[1])
                            {
                                projectName = xmlNode.Name;
                                codeClass = xmlElement.Name;
                                flag = true;
                                break;
                            }
                        }
                    }
                }
                comboBox1.Text = projectName;
                comboBox2.Text = codeClass;
                comboBox3.Text = codeName;
            }
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            string projectName = comboBox1.Text;
            XmlNode childNode2 = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName);
            XmlNodeList childNodeList2 = childNode2.ChildNodes;
            List<string> list2 = new List<string>();
            foreach (XmlNode child1Node in childNodeList2)
            {
                list2.Add(child1Node.Name);
            }
            comboBox2.DataSource = list2;

            string codeClass = comboBox2.Text;
            XmlNode childNode3 = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(codeClass);
            XmlNodeList childNodeList3 = childNode3.ChildNodes;
            List<string> list3 = new List<string>();
            foreach (XmlNode child1Node in childNodeList3)
            {
                list3.Add(child1Node.SelectSingleNode("项目编码").InnerText + " " + child1Node.SelectSingleNode("项目名称").InnerText);
            }
            comboBox3.DataSource = list3;
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            string projectName = comboBox1.Text;
            string codeClass = comboBox2.Text;
            XmlNode childNode3 = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(codeClass);
            XmlNodeList childNodeList3 = childNode3.ChildNodes;
            List<string> list3 = new List<string>();
            foreach (XmlNode child1Node in childNodeList3)
            {
                list3.Add(child1Node.SelectSingleNode("项目编码").InnerText + " " + child1Node.SelectSingleNode("项目名称").InnerText);
            }
            comboBox3.DataSource = list3;
        }
    }
}
